package com.shopping.service.message;

import com.shopping.constant.ResponseCode;
import com.shopping.dao.MessageDao;
import com.shopping.dao.RecMessageDao;
import com.shopping.pojo.Message;
import com.shopping.pojo.RecMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    RecMessageDao recMessageDao;
    @Autowired
    MessageDao messageDao;

    @Override
    public Map<String, Object> addMessage(RecMessage recMessage, String context) {
        Map<String, Object> map = new HashMap<>();
        try {
            int id;
            RecMessage r = recMessageDao.selectByIds(recMessage.getMyId(), recMessage.getUserId());
            if(r == null){
                recMessageDao.insert(recMessage);
                id = recMessageDao.lastInsertId();
            }else{
                id = r.getId();
            }
            messageDao.insert(new Message(id, context));
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteMessage(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            RecMessage r = recMessageDao.selectById(id);
            messageDao.deleteAll(r.getId());
            recMessageDao.deleteById(id);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteAllMessage(int myId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<RecMessage> recMessageList = recMessageDao.selectByMyId(myId);
            for(RecMessage r: recMessageList){
                messageDao.deleteAll(r.getId());
            }
            recMessageDao.deleteByMyId(myId);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> showMessages(int userId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<RecMessage> recMessageList = recMessageDao.selectByMyId(userId);
            for(RecMessage r: recMessageList){
                List<Message> messageList = messageDao.selectById(r.getId());
                r.setMessageList(messageList);
            }
            map.put("messages", recMessageList);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }
}
